﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using justice.Models;
using justice.ViewModel;

namespace justice.Controllers
{
    public class XKDShroffTrustBookController : Controller
    {
        // GET: XKDShroffTrustBook 西科大鉴定委托书评审记录
        justiceEntities dal = new justiceEntities();

        //根据西科大鉴定委托书评审记录id查单个信息
        public ActionResult ShroffTrustDataView(int? id)
        {
            Shroff_book book = dal.Shroff_book.ToList().Find(l => l.shroff_id == id);

            be_appraiser beapper = dal.be_appraiser.ToList().Find(l => l.user_id == book.main_case);

            inserer iner = dal.inserer.ToList().Find(l => l.insurer_id == book.insurer);

            justice_user user = dal.justice_user.ToList().Find(l => l.id == book.enter_user_id);

            judiciary jdc = dal.judiciary.ToList().Find(l => l.id == user.unit_id);

            Role rol = dal.Role.ToList().Find(l => l.RoleName == "管理员");

            justice_user fuuser = dal.justice_user.ToList().Find(l => l.unit_id == jdc.id && l.role_id == rol.id);

            Role role = dal.Role.ToList().Find(l => l.id == user.role_id);

            ViewBag.number = book.shroff_number;//表格编号
            ViewBag.clicent = book.client;//委托人
            ViewBag.bename = beapper.username;//被委托人
            ViewBag.user = user.userName;//评审人、鉴定人
            ViewBag.fuuser = fuuser.userName;
            ViewBag.shroffid = book.shroff_id;

            if (book.insurer == null)
            {
                ViewBag.ner = "未涉保";
            }
            else if (book.insurer != null)
            {
                ViewBag.ner = iner.insurerName;
            }

            //查记录
            var list = from tu in dal.ShroffTrust
                       join books in dal.Shroff_book on tu.shroffid equals books.shroff_id
                       where id == tu.shroffid
                       select new shroffTrust_insurer
                       {
                           firstdraftwrite = tu.FirstDraftWrite,
                           reviewpeople = tu.ReviewPeople,
                           reviewtype = tu.ReviewTpyeId,
                           protocolreviewcontent = tu.ProtocolReviewContent,
                           protocolreviewtabb = tu.ProtocolReviewTabb,
                           appraiseridea = tu.AppraiserIdea,
                           organizationidea = tu.OrganizationIdea,
                           appraiserdateTime = tu.apptime,//鉴定人签字时间
                           organizationdatetime = tu.organtime,//负责机构签字时间
                           insurerorjoin = tu.InsurerOrJoin,
                           insureridea = tu.InsurerIdea,
                       };
            return View(list.ToList()[0]);
        }

        //添加其他信息
        public ActionResult UpdateShroffTrustInfo(ShroffTrust shroffTrust)
        {
            ShroffTrust trust = dal.ShroffTrust.ToList().Find(l => l.shroffid == shroffTrust.shroffid);
            trust.FirstDraftWrite = shroffTrust.FirstDraftWrite;
            trust.ReviewTpyeId = shroffTrust.ReviewTpyeId;
            trust.ProtocolReviewContent = shroffTrust.ProtocolReviewContent;
            trust.ProtocolReviewTabb = shroffTrust.ProtocolReviewTabb;
            trust.AppraiserIdea = shroffTrust.AppraiserIdea;
            trust.OrganizationIdea = shroffTrust.OrganizationIdea;
            trust.InsurerOrJoin = shroffTrust.InsurerOrJoin;
            trust.InsurerIdea = shroffTrust.InsurerIdea;
            trust.apptime = shroffTrust.apptime;//鉴定人签字时间
            trust.organtime = shroffTrust.organtime;//负责机构签字时间
            trust.shroffid = shroffTrust.shroffid;
            int i = dal.SaveChanges();
            if (i > 0)
            {
                Response.Redirect("/XKDShroffTrustBook/UpdateShroffTrustView?id=" + trust.shroffid);
            }
            ModelState.AddModelError("UpdateShroffTrustError", "更新失败！");
            return View();
        }


        //添加其他信息显示页
        public ActionResult UpdateShroffTrustView(int? id)
        {
            Shroff_book book = dal.Shroff_book.ToList().Find(l => l.shroff_id == id);

            be_appraiser beapper = dal.be_appraiser.ToList().Find(l => l.user_id == book.main_case);

            inserer iner = dal.inserer.ToList().Find(l => l.insurer_id == book.insurer);

            justice_user user = dal.justice_user.ToList().Find(l => l.id == book.enter_user_id);

            judiciary jdc = dal.judiciary.ToList().Find(l => l.id == user.unit_id);

            Role rol = dal.Role.ToList().Find(l => l.RoleName == "管理员");

            justice_user fuuser = dal.justice_user.ToList().Find(l => l.unit_id == jdc.id && l.role_id == rol.id);

            Role role = dal.Role.ToList().Find(l => l.id == user.role_id);


            ViewBag.number = book.shroff_number;
            ViewBag.clicent = book.client;
            ViewBag.bename = beapper.username;
            ViewBag.user = user.userName;
            ViewBag.fuuser = fuuser.userName;
            ViewBag.shroffid = book.shroff_id;
            if (book.insurer == null)
            {
                ViewBag.ner = "未涉保";
            }
            else if (book.insurer != null)
            {
                ViewBag.ner = iner.insurerName;
            }

            return View();
        }

        //del
        public ActionResult DelShroffTrustInfo(int id)
        {
            ShroffTrust shroff = dal.ShroffTrust.ToList().Find(l => l.Shrofftrust_Id == id);
            if (shroff != null)
            {
                dal.ShroffTrust.Remove(shroff);
            }
            else
            {
                dal.SaveChanges();
            }
            ViewBag.shroffList = dal.ShroffTrust.ToList();
            return View();
        }

        //打印excel文档
        [HttpPost]
        public ActionResult ExportExcel(FormCollection form)
        {
            string strHtml = form["hHtml"];
            strHtml = HttpUtility.HtmlDecode(strHtml);
            byte[] b = System.Text.Encoding.Default.GetBytes(strHtml);
            return File(b, "application/vnd.ms-excel", "这是Excel.xls");
        }

    }
}